//
//  BSMainTabBarController.swift
//  mySwiftWeibo
//
//  Created by apple on 16/7/18.
//  Copyright © 2016年 itcast沈庆伟. All rights reserved.
//

import UIKit

class BSMainTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        //self.tabBar.tintColor=UIColor.orangeColor()
        
        
        //自定义tabBar
        let mainTabBar=BSMainTabBar()
        
        //定义并传递闭包给mainTabBar
        mainTabBar.composeCallBack={() -> Void in
           //print("我在控制器里面知道加号按钮点击了")
            
            //TODO
            
        }
        
        //self.tabBar=mainTabBar  readonly不能直接赋值
        //KVC间接赋值
        self.setValue(mainTabBar, forKey: "tabBar")
        
        self.addchildviewcontrollers()
    }
    

}

extension BSMainTabBarController{
    
    ///添加所有子控制器
    func addchildviewcontrollers() {
        //首页
        let homeVC=BSHomeTableViewController()
        myAddChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        //消息
        let messageVC=BSMessageTableViewController()
        myAddChildViewController(messageVC, title: "消息", imageName: "tabbar_message_center")
        //发现
        let discoveryVC=BSDiscoveryTableViewController()
        myAddChildViewController(discoveryVC, title: "发现", imageName: "tabbar_discover")
        //我
        let profileVC=BSProfileTableViewController()
        myAddChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
    }
    
    ///抽取:添加子控制器方法
    func myAddChildViewController(controller: UIViewController,title: String,imageName: String) {
        
        controller.title=title
        //设置标题选中的颜色
        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.orangeColor()], forState: UIControlState.Selected)
        
        controller.tabBarItem.image=UIImage(named: imageName)
        //拼接选中状态的图片的名称
        let selectedImageName = imageName + "_selected"
        controller.tabBarItem.selectedImage=UIImage(named: selectedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //添加子控制器,并附带导航栏
        self.addChildViewController(UINavigationController(rootViewController: controller))
        
    }

}
